<template>
  <el-dialog
    v-bind="$attrs"
    width="1000px"
    :title="dialogForm.id ? '修改国家质检中心' : '新增国家质检中心'"
    fullscreen
    center
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      label-width="auto"
      :disabled="isDetail"
    >
      <el-form-item label="依托法人单位" prop="relyingLegalEntity">
        <el-input v-model="dialogForm.relyingLegalEntity" />
      </el-form-item>
      <el-form-item label="机构名称" prop="institutionName">
        <el-input v-model="dialogForm.institutionName" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="contactPhone">
        <el-input v-model="dialogForm.contactPhone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dialogForm.address" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="onclose()">取消</el-button>
        <el-button type="primary" @click="submit(true)" v-if="!isDetail">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { defineComponent, reactive, ref, toRefs, watch, computed } from "vue";
import Editor from "@/components/Editor/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import {
  addQualityMapInfo,
  getQualityMapInfoByAdmin,
  modifyQualityMapInfo,
} from "@/api/api";
export default defineComponent({
  components: { Editor, FileUpload },
  props: {
    dataForm: Object,
    isDetail: {
      type: Boolean,
      default: false,
    },
    dataType: {
      type: [String, Number],
      default: "",
    },
    dataContext: {
      type: [String, Number],
      default: "",
    },
  },
  setup(props, { emit }) {
    const dialogFormRef = ref();
    const dataMap = reactive({
      dialogForm: {},
      rules: {
        relyingLegalEntity: [
          { required: true, message: "请输入依托法人单位", trigger: "blur" },
        ],
        institutionName: [
          { required: true, message: "请输入机构名称", trigger: "blur" },
        ],
        contactPhone: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
    });
    const methodsMap = reactive({
      onclose() {
        emit("close");
      },
      submit(flag) {
        dialogFormRef.value.validate((valid) => {
          if (valid) {
            let params = {
              ...dataMap.dialogForm,
              dataType: props.dataType,
              dataContext: props.dataContext,
            };
            if (dataMap.dialogForm.id) {
              modifyQualityMapInfo(params).then((res) => {
                console.log("修改成功", Date.now());
                setTimeout(() => {
                  emit("getDatas");
                  emit("close");
                }, 500);
              });
            } else {
              addQualityMapInfo(params).then((res) => {
                console.log("新增成功", Date.now());
                setTimeout(() => {
                  emit("getDatas");
                  emit("close");
                }, 500);
              });
            }
          } else {
            return false;
          }
        });
      },
    });
    onMounted(() => {
      let defaultData = {};
      if (props.dataForm) {
        let temp = JSON.parse(JSON.stringify(props.dataForm));
        dataMap.dialogForm = {
          ...defaultData,
          ...temp,
        };
      } else {
        dataMap.dialogForm = {
          ...defaultData,
        };
      }
    });
    return {
      ...toRefs(dataMap),
      ...toRefs(methodsMap),
      dialogFormRef,
    };
  },
});
</script>
